package cn.shenning.dui.kaobei;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 * Created by 白夜行 on 2017/5/13.
 */
public class DeepCopyServiable {
    public static void main(String[] args) throws Exception {
        Teacher4 t = new Teacher4();
        t.setName("Taylor");
        t.setAge(28);

        Student4 s1 = new Student4();
        s1.setAge(20);
        s1.setName("blank space");
        s1.setTeacher(t);

        Student4 s2 = (Student4) s1.deepClone();

        System.out.println("拷贝后:");
        System.out.println(s2.getName());
        System.out.println(s2.getAge());
        System.out.println(s2.getTeacher().getName());
        System.out.println(s2.getTeacher().getAge());
        System.out.println("---------------------------");

        t.setName("swift");

        System.out.println("修改后：");
        System.out.println(s1.getTeacher().getName());
        System.out.println(s2.getTeacher().getName());
    }

}

class Teacher4 implements Serializable
{
    private String name;
    private int age;

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public int getAge()
    {
        return age;
    }

    public void setAge(int age)
    {
        this.age = age;
    }

}

class Student4 implements Serializable
{
    private String name;
    private int age;
    private Teacher4 teacher;

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public int getAge()
    {
        return age;
    }

    public void setAge(int age)
    {
        this.age = age;
    }

    public Teacher4 getTeacher()
    {
        return teacher;
    }

    public void setTeacher(Teacher4 teacher)
    {
        this.teacher = teacher;
    }

    public Object deepClone() throws Exception
    {
        // 序列化
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);

        oos.writeObject(this);

        // 反序列化
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);

        return ois.readObject();
    }

}